﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

using SSTU.Helper;

namespace SSTU.ViewModel.Core
{
	/// <summary>
	/// Base class for all view models.
	/// </summary>
	public class ViewModelBase : INotifyPropertyChanging, INotifyPropertyChanged
	{
		/// <summary>
		/// Raises the property changing.
		/// </summary>
		protected void RaisePropertyChanging<T>(Expression<Func<T>> propertyExpression)
		{
			string propertyName = PropertyHelper.ExtractPropertyName<T>(propertyExpression);
			if (PropertyChanging != null)
			{
				PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
			}
		}

		/// <summary>
		/// Raises the property changed.
		/// </summary>
		protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
		{
			string propertyName = PropertyHelper.ExtractPropertyName<T>(propertyExpression);
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		/// <summary>
		/// Occurs when a property value is changing.
		/// </summary>
		public event PropertyChangingEventHandler PropertyChanging;

		/// <summary>
		/// Occurs when a property value changes.
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;
	}
}
